# Definition for a binary tree node.
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None


class CBTInserter:
    def __init__(self, root: TreeNode):
        self.queue = []
        self.root = root
        self.queue = []
        queue = [root]
        append = False
        while queue:
            node = queue.pop(0)
            if node.left is None or node.right is None:
                append = True
            if node.left:
                queue.append(node.left)
            if node.right:
                queue.append(node.right)
            if append:
                self.queue.append(node)

    def insert(self, v: int) -> int:
        node = TreeNode(v)
        root = self.queue[0]
        if root.left:
            self.queue.pop(0)
            root.right = node
        else:
            root.left = node
        self.queue.append(node)
        return root.val

    def get_root(self) -> TreeNode:
        return self.root
